
#include "EZTcp.h"
#include "EZWebsocketServer.h"
#include "EZWebsocketCB.h"
#include "EZWebsocketCon.h"
#include "EZWebsocketMsg.h"
#include "client/EZConnecter.h"
#include "EZWebsocketClient.h"
#include "loop/EZTimer.h"
#include "transport/EZNoTlsTransport.h"
#include "transport/EZTlsTransport.h"
#include "EZRPC_Server.h"
#include "EZRPC_Channel.h"

#include "echo.pb.h"
#include "echo.pb.cc"

#include <thread>

#ifdef _DEBUG
#pragma comment(lib,"libprotobuf_d.lib")
#pragma comment(lib,"../bin/EZRPC_d.lib")
#else
#pragma comment(lib,"libprotobuf.lib")
#pragma comment(lib,"../bin/EZRPC.lib")
#endif

int svrc = 0;
int cltc = 0;

class EchoImp : public EchoServer
{
	virtual void Echo(::google::protobuf::RpcController* controller,
		const ::EchoRequest* request,
		::EchoResponse* response,
		::google::protobuf::Closure* done)
	{
		svrc++;
		//std::cout << "server recv:" << request->message() << std::endl;
		response->set_message("replay - " + request->message());
		done->Run();
	}
};

int main2()
{
	EZTcp::EZLoop lp;

	EZRPC_Server ser;
	ser.register_server(new EchoImp);
	ser.start(18888);

	::EchoRequest* request = new ::EchoRequest;
	::EchoResponse* response = new ::EchoResponse;

	request->set_message("hello world");

	EZRPC_Channel rc;
	rc.connect("ws://127.0.0.1:8888/");
	EchoServer_Stub cs(&rc);

	auto tmr = EZTcp::EZTimer::create(&lp,1000,true);
    tmr->start([&](const std::error_code& e) {
        EZLOG_INFO << "server count: " << svrc << " client count: " << cltc;
        svrc = cltc = 0;
    });

	std::thread([&](){lp.run();}).detach();

	auto *cntl = new EZRPC_Ctl;
	while (1)
	{
		cntl->set_timeout(5000);
		cs.Echo(cntl, request, response, nullptr);
		if (cntl->Failed()){
			std::cout << cntl->ErrorText() << std::endl;
		}else{
			cltc++;
		}
	}

	return 0;
}